﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EBSystem.Enum;
using EBSystem.SQL;
using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace EBSystem.ViewModels
{
    public class LoginViewModel : ObservableObject
    {
        private string? userNameInput;
        public string? UserNameInput
        {
            get => userNameInput;
            set
            {
                userNameInput = value;
                OnPropertyChanged();
                InputCheck();
            }
        }

        private string? userPasswordInput;
        public string? UserPasswordInput
        {
            get => userPasswordInput;
            set
            {
                userPasswordInput = value;
                OnPropertyChanged();
                InputCheck();
            }
        }

        private bool loginIsEnabled = false;
        public bool LoginIsEnabled
        {
            get => loginIsEnabled;
            set
            {
                loginIsEnabled = value;
                OnPropertyChanged();
            }
        }

        public void InputCheck()
        {
            if (UserNameInput is null || UserNameInput == "" ||
                UserPasswordInput is null || UserPasswordInput == "")
            {
                LoginIsEnabled = false;
                return;
            }
            LoginIsEnabled = true;
        }

        public long Login()
        {
            long accountID = SQLConnection.LoginAuthentication(UserNameInput, UserPasswordInput);
            if (accountID >= 0)
            {
                UserNameInput = "";
                UserPasswordInput = "";
                InputCheck();
                return accountID;
            }
            else
            {
                return accountID;
            }
        }
    }
}
